package com.woniu.sharepark.util;

import com.baidu.aip.ocr.AipOcr;
import com.woniu.sharepark.leasesystem.common.dto.IDCardDTO;
import com.woniu.sharepark.leasesystem.common.dto.UserInfoDTO;
import com.woniu.sharepark.leasesystem.common.pojo.DrivingPermit;
import org.json.JSONObject;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.HashMap;

/**
 * @ClassName: BaiduVerifyUtil
 * @Description: 此工具封装了百度识别接口
 * @Author: Zongjie.Li
 * @Date: 2019/6/3
 * @Version: 1.0
 **/
public class BaiduVerifyUtil {
    //以下三个变量注册百度云账号后创建应用即可获取

    public static final String APP_ID = "16386597";
    public static final String API_KEY = "PetlSAGWj7G8mVzDGVNSeQtR";
    public static final String SECRET_KEY = "uTebaOWUW2eoid32eOxUGEodacKrTcIv";
    public static final AipOcr CLIENT = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

    public static UserInfoDTO idcardRecognition(IDCardDTO idCardDTO, UserInfoDTO userInfoDTO) {
        //为传入的可选参数，可在文档内查看
        HashMap<String, String> options = new HashMap<String, String>();
        //图片图像可以传入本地图像路径，也可以传入base64二进制编码(不知道是啥)
        try {
            byte[] front = idCardDTO.getFrontImage().getBytes();
            JSONObject frontInfo = CLIENT.idcard(front, "front", options);
            System.out.println(frontInfo.toString());
            byte[] back = idCardDTO.getBackImage().getBytes();
            JSONObject backInfo = CLIENT.idcard(back, "back", options);
            System.out.println(backInfo.toString());
            userInfoDTO.setName(frontInfo.getJSONObject("words_result").getJSONObject("姓名").get("words").toString());
            if (!frontInfo.getJSONObject("words_result").isNull("性别")) {
                userInfoDTO.setSex(frontInfo.getJSONObject("words_result").getJSONObject("性别").get("words").toString());
            }
            userInfoDTO.setCard(frontInfo.getJSONObject("words_result").getJSONObject("公民身份号码").get("words").toString());
            String time = backInfo.getJSONObject("words_result").getJSONObject("失效日期").get("words").toString();
            if (time != null && !time.equals("")) {
                userInfoDTO.setInvalidateTime(new SimpleDateFormat("yyyyMMdd").parse(time));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return userInfoDTO;
    }

    /**
    * 行驶证图文识别
    * @title drivingPermitCard
    * @param drivingImg
    * @return com.woniu.sharepark.leasesystem.common.pojo.DrivingPermit
    * @author DELL
    * @date 2019/6/10 10:05
    **/

    public static DrivingPermit drivingPermitCard(MultipartFile drivingImg){
        HashMap options = new HashMap();
        DrivingPermit drivingPermit = new DrivingPermit();
        try{
        byte[] front = drivingImg.getBytes();
        JSONObject frontInfo = CLIENT.vehicleLicense(front,options);
        drivingPermit.setDriveCode(frontInfo.getJSONObject("words_result").getJSONObject("车辆识别代号").get("words").toString());
        drivingPermit.setDriveAddress(frontInfo.getJSONObject("words_result").getJSONObject("住址").get("words").toString());
        drivingPermit.setDriveRanges(frontInfo.getJSONObject("words_result").getJSONObject("品牌型号").get("words").toString());
        drivingPermit.setDriveStarttime(frontInfo.getJSONObject("words_result").getJSONObject("发证日期").get("words").toString());
        drivingPermit.setDriveType(frontInfo.getJSONObject("words_result").getJSONObject("车辆类型").get("words").toString());
        drivingPermit.setDriveCharge(frontInfo.getJSONObject("words_result").getJSONObject("所有人").get("words").toString());
        drivingPermit.setDriverNature(frontInfo.getJSONObject("words_result").getJSONObject("使用性质").get("words").toString());
        drivingPermit.setDriverEngine(frontInfo.getJSONObject("words_result").getJSONObject("发动机号码").get("words").toString());
        drivingPermit.setDriverPlate(frontInfo.getJSONObject("words_result").getJSONObject("号牌号码").get("words").toString());
        drivingPermit.setDriverRegistrationtime(frontInfo.getJSONObject("words_result").getJSONObject("注册日期").get("words").toString());


        }catch (Exception e){

        }
        return drivingPermit;
    }



}

